var base_url = "http://baich-day.com/";
/*!
 * Start Bootstrap - Agnecy Bootstrap Theme (http://startbootstrap.com)
 * Code licensed under the Apache License v2.0.
 * For details, see http://www.apache.org/licenses/LICENSE-2.0.
 */

// jQuery for page scrolling feature - requires jQuery Easing plugin
$(function() {
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 1500, 'easeInOutExpo');
        event.preventDefault();
    });
});

// Highlight the top nav as scrolling occurs
$('body').scrollspy({
    target: '.navbar-fixed-top'
})

// Closes the Responsive Menu on Menu Item Click
$('.navbar-collapse ul li a').click(function() {
    $('.navbar-toggle:visible').click();
});

//
function sort_ads(url,cat) {
	var sort_by = document.getElementById('sort_by').value;
	var sort_order = document.getElementById('sort_order').value;
    //alert(url+"Site/category/"+sort_by+"/"+sort_order+"/");
	window.location.assign(url+"Site/category/"+sort_by+"/"+sort_order+"/"+cat+"/");
}
function sort_notes(url) {
	var sort_by = document.getElementById('sort_by').value;
	var sort_order = document.getElementById('sort_order').value;
    //alert(url+"Site/category/"+sort_by+"/"+sort_order+"/");
	window.location.assign(url+"Site/category_notes/"+sort_by+"/"+sort_order+"/");
}

function sort_users(url,search) {
    var sort_by = document.getElementById('sort_by').value;
    var sort_order = document.getElementById('sort_order').value;
    //alert(url+"Site/search_users/"+sort_by+"/"+sort_order+"/");
    window.location.assign(url+"Site/search_users/"+sort_by+"/"+sort_order+"/"+search+"/");
}

function search_rout(url){
    var search_q = document.getElementById('search_q').value;
    var select = document.getElementById('sel').value;
    
    if(search_q==""){
        return;
    }
    if(select == "Ads"){
        window.location.assign(url+"Site/search_ads/0/0/"+search_q+"/");
    }   
    else if(select == "Notes"){
        window.location.assign(url+"Site/search_notes/0/0/"+search_q+"/");
    }   
    else if(select == "Users"){
        window.location.assign(url+"Site/search_users/0/0/"+search_q+"/");
    }   
}

$("#search_q").keyup(function(event){
    if(event.keyCode == 13){
        $("#search_btn").click();
    }
});

$("#commentBox").keyup(function(event){
    if (event.keyCode == 13 && event.shiftKey) {
        this.value += '\n';
    }
    else if(event.keyCode == 13){
        $("#commentBtn").click();
    }

});

$(document).ready(function() {
    $('#commentBtn').click(function(){
        comment_click();
    });
});

function comment_click(){
     //get text
        var _comment = $('#commentBox').val();
        var _userId =$('#userId').val();
        var _adId = $('#ad_id').val();
        var _name = $('#fname').val();
        var url = $('#url').val();

        if(_comment.length > 0)
        {
            //alert(_comment+" "+_userId);
            $.post( base_url+"members/insert_comment/",
                    {
                        task : "comment_insert",
                        userId : _userId,
                        adId :  _adId,
                        name : _name,
                        comment : _comment
                    }                    
                )
                .error(
                    function(){
                        console.log("Error!");
                    })
                .success(
                        function(data){
                         //insert into db   
                        insert_com( jQuery.parseJSON(data) );
                        console.log("response: "+data);
                    });
        }
        
        //empty text box
        $('#commentBox').val("");
}    

function insert_com(data){
    var com ="";
     com +="<div class='row'>";
     com +="<div class='col-md-1'>";
     com +="<div class='pull-left'>";
     com +="<div class='img-div-xsml'>";
     com +="<img src='"+data.dp+"'/>";
     com +="</div>";
     com +="<b>"+data.userName+"</b>";
     com +="</div>";  
     com +="</div>";                                  
     com +="<div class='col-md-11'>";                             
     com +="<p>"+data.comment+"</p>";                                        
     com +="<a href='#''><b>Delete</b></a>";                                     
     com +="</div>";                                  
     com +="<hr >";                                    
     com +="</div><br>";                            
                                
                             
    $('#comments').after(com);
}
